package ywpt.web.action.knowledge;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.KnowMsg;
import ywpt.model.PageResultSet;
import ywpt.service.knowledge.KnowMsgService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class KnowMsgAction extends BaseAction implements
ModelDriven<KnowMsg>, Preparable{

	/**
	 * 给知识点留言
	 */
	private static final long serialVersionUID = 662185173100513841L;
	private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	private Long id;
	private Long knowLibId;
	
	private Long peopleId;
	private Long departId;
	private Long positionId;
	
	private KnowMsg knowMsg;
	private KnowMsgService  knowMsgService;
	
	private int page;  //接受传递的页码参数

	private PageResultSet<KnowMsg> pageResultSet;


	
	
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageResultSet<KnowMsg> getPageResultSet() {
		return pageResultSet;
	}

	public void setPageResultSet(PageResultSet<KnowMsg> pageResultSet) {
		this.pageResultSet = pageResultSet;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getKnowLibId() {
		return knowLibId;
	}

	public void setKnowLibId(Long knowLibId) {
		this.knowLibId = knowLibId;
	}

	public Long getPeopleId() {
		return peopleId;
	}

	public void setPeopleId(Long peopleId) {
		this.peopleId = peopleId;
	}

	public Long getDepartId() {
		return departId;
	}

	public void setDepartId(Long departId) {
		this.departId = departId;
	}

	public Long getPositionId() {
		return positionId;
	}

	public void setPositionId(Long positionId) {
		this.positionId = positionId;
	}

	public KnowMsg getKnowMsg() {
		return knowMsg;
	}

	public void setKnowMsg(KnowMsg knowMsg) {
		this.knowMsg = knowMsg;
	}

	public KnowMsgService getKnowMsgService() {
		return knowMsgService;
	}

	public void setKnowMsgService(KnowMsgService knowMsgService) {
		this.knowMsgService = knowMsgService;
	}	
	
	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	
	@Override
	public KnowMsg getModel() {
		return this.knowMsg;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.knowMsg = this.knowMsgService.getKnowMsgById(id);
		}else{
			this.knowMsg = new KnowMsg();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.KNOW_LIB);
	}
	
	/**
	 * ����,����,�ύ
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		try 
		{
			if (id == null) 
			{
				knowMsg.setMsgDate(FORMAT.parse(FORMAT.format(new Date())));
				if(this.getCurrentUser().getPeople()!=null){
					this.setPeopleId(this.getCurrentUser().getPeople().getId());
					this.setDepartId(this.getCurrentUser().getPeople().getDepart().getId());
					this.setPositionId(this.getCurrentUser().getPeople().getPosition().getId());					
				}
				this.knowMsgService.saveKnowMsg(knowMsg,knowLibId,peopleId,departId,positionId);
			} 
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
				this.addActionError(e.getMessage());
			return INPUT;
		}
	}

	public String getMessings() {
		DetachedCriteria  query = DetachedCriteria.forClass(KnowMsg.class);
			if(this.knowLibId!=null){
				query.createAlias("knowLib", "knowLib").add(Restrictions.eq("knowLib.id", knowLibId));
			}
		this.pageResultSet = this.knowMsgService.queryByPage(6, page, query); // ��ʾ��pageҳ����ʾ15����¼
		return SUCCESS;
	}

	public String delete() {
		if(id!=null){
			this.knowMsgService.removeMsg(id);
			return SUCCESS;
		}else{
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
		
	}

	

}
